﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

public partial class ProductRepair_RepairAdd : System.Web.UI.Page
{
    private Repair rep;
    DataTable sNdt, stepdt;


    public ProductRepair_RepairAdd()
    {
        rep = new Repair();
        sNdt = new DataTable();
        stepdt = new DataTable();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Button5.Attributes.Add("onclick", "PasteData()");
            BindFlowStep();
        }
    }

    public void BindFlowStep()
    {
        this.CheckBoxList1.DataSource = FlowStep.list;
        this.CheckBoxList1.DataTextField = "StepText";
        this.CheckBoxList1.DataValueField = "StepValue";
        this.CheckBoxList1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SNAdd();
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        SNAdd();
    }

    protected void SNAdd()
    {
        string productSN = this.TextBox2.Text.Trim();
        if (productSN.Length < 10)
        {
            string str1 = "序列号过短，请检查！";
            //ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (productSN.Length > 100)
        {
            string str1 = "序列号过长，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (ProductSNCompare(productSN))
        {
            string str1 = "序列号已在列表中！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            ListBox1.Items.Insert(0, productSN);
            this.Label1.Text = this.ListBox1.Items.Count.ToString();
            this.TextBox2.Text = "";
            this.TextBox2.Focus();
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        string str = this.myVal.Value;
        string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string st in strs)
        {
            if (!ProductSNCompare(st))
                this.ListBox1.Items.Add(st.Trim());
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected bool ProductSNCompare(string psn)
    {
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Text == psn)
            {
                return true;
            }
        }
        return false;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ListItemCollection lic = new ListItemCollection();
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Selected)
                lic.Add(item);
        }
        foreach (ListItem item in lic)
        {
            this.ListBox1.Items.Remove(item);
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.Items.Count == 0)
        {
            string str1 = "请先添加序列号！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (this.CheckBoxList1.SelectedIndex < 0)
        {
            string str1 = "请选择要返修的工序！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            BiuldSNDT();
            BiuldStepDT();
            DataInsert();
        }
    }

    protected void BiuldSNDT()
    {
        sNdt.Columns.Add("ProductSN");
        DataRow dr;
        foreach (ListItem item in this.ListBox1.Items)
        {
            dr = sNdt.NewRow();
            dr["ProductSN"] = item.Text;
            sNdt.Rows.Add(dr);
        }
    }

    protected void BiuldStepDT()
    {
        stepdt.Columns.Add("RepairStep");
        DataRow dr;
        foreach (ListItem item in this.CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                dr = stepdt.NewRow();
                dr["RepairStep"] = item.Text;
                stepdt.Rows.Add(dr);
            }
        }
    }

    protected void DataInsert()
    {
        if (rep.RepairItem_Insert(DateTime.Now,User.Identity.Name,this.TextBox3.Text.Trim(),sNdt,stepdt))
        {
            string str1 = "信息插入成功！这一批共有" + sNdt.Rows.Count + "个序列号。";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            string str1 = "信息插入失败！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        sNdt.Dispose();
        stepdt.Dispose();
    }
}
